package com.hxkj.controller.taglib;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import com.hxkj.domain.po.FreeMarker;
import com.hxkj.service.taglib.AnalyzeChannelService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultMessageStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * @ClassName ColumnArticleListController
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/7/18 11:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/analyze")
public class AnalyzeChannelController {

    @Autowired
    private AnalyzeChannelService analyzeChannelService;

   /**
   * @Description analyzeChannel 解析频道自定义标签接口
   **/
    @RequestMapping(value = "/analyzeChannel", method = RequestMethod.POST)
    public Callable<JSONObject> analyzeChannel(@ModelAttribute FreeMarker freeMarker) throws Exception{
        return () -> analyzeChannelService.analyzeChannel(freeMarker);
    }

    /**
     * 多个频道静态页生成 chenfangzhou
     * @param freeMarker
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/createChannelPages")
    public Callable<ResultMessageStruct> create(@ModelAttribute FreeMarker freeMarker) throws Exception {
        return () -> {
            ResultMessageStruct result = new ResultMessageStruct();
            try {
                analyzeChannelService.createChannelPages(freeMarker);
                result.setMessage("成功");
                result.setCode(200);
            }catch (CustomException e){
                result.setMessage(e.getMessage());
                result.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                result.setMessage(e.getMessage());
                result.setCode(e.getCode());
            }catch (Exception e){
                Log.error("生成多个频道静态文件出错",e);
                result.setMessage("生成多个频道静态文件出错");
                result.setCode(400);
            }
            return result;
        };
    }
}
